package com.xpf.service.status;

import com.xpf.service.status.entity.BpmProcessSuccessEvent;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class StatusServiceFactory {

    private Map<String, IStatusProcessService<BpmProcessSuccessEvent>> statusProcessMapGroup = new ConcurrentHashMap<>();

    public StatusServiceFactory(List<IStatusProcessService<BpmProcessSuccessEvent>> statusProcessServices) {
        statusProcessServices.forEach(statusProcessService -> {
            BpmStatusStrategy bpmStatusStrategy = AnnotationUtils.findAnnotation(statusProcessService.getClass(), BpmStatusStrategy.class);
            if (null != bpmStatusStrategy) {
                statusProcessMapGroup.put(bpmStatusStrategy.statusModel().getCode(), statusProcessService);
            }
        });
    }


    public Map<String, IStatusProcessService<BpmProcessSuccessEvent>> openStatusProcess() {
        return statusProcessMapGroup;
    }

    @AllArgsConstructor
    @Getter
    public enum StatusModel {

        SUBMITTED("SUBMITTED", "提交"),

        RUNNING("RUNNING", "运行中"),

        SUSPENDED("SUSPENDED", "暂停"),

        COMPLETED("COMPLETED", "完结"),

        TERMINATED("TERMINATED", "终止"),

        CANCELED("CANCELED", "流程发起人关闭"),

        UN_KNOWN("UN_KNOWN", "匹配出错, 未知类型");

        private final String code;
        private final String desc;
    }
}

